<?php
class OrdersController extends AppController {
    var $name = 'Orders';
    var $components = array('Otp', 'Auth', 'RecaptchaPlugin.Recaptcha');
    var $uses = array('User', 'Deal', 'Order', 'Buyhistory');
    var $helpers = array('Form', 'RecaptchaPlugin.Recaptcha', 'Thumb'); 

    function view($id) {
        $order = $this->Order->findById($id);
        $user = $this->Auth->user();

        if ($order['Order']['user_id'] != $user['User']['id']) {
            $this->Session->setFlash('Invalid request');
            $this->redirect('/');
        }

        $buyhistory = $this->Buyhistory->find('all', array(
            'conditions' => array('order_id' => $id)
        ));
        $product_id = array();
        $deal_id = array();
        foreach($buyhistory as $item) {
            $product_id[] = $item['Buyhistory']['product_id'];
            $deal_id[] = $item['Buyhistory']['deal_id'];
        }

        $products = $this->Product->find('all', array(
            'conditions' => array('Product.id' => $product_id)
        ));
        $deals = $this->Deal->find('all', array(
            'conditions' => array('Deal.id' => $deal_id)
        ));
        $this->set(compact(array('buyhistory', 'products', 'deals', 'user')));
    }
}
